const util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pdgl_pk: '',//派单管理pk
    biz_1_pdgl_0: [],//行程明细
    biz_1_pdgl_1: [],//参训人员
    biz_1_pdgl_2: [],//后勤保障
    HEADER: {},
    index: null,//第几行的下标
    dataName: null,//
    sdList: [],//时段枚举
    selectpointShow: false,//选择资源点弹窗
    xbList: [//性别枚举
      {
        value: 1,
        name: '男'
      }, {
        value: 2,
        name: '女'
      }
    ],
    cyList: [],//餐饮枚举
    zsList: [],//住宿枚举
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    const { pdgl_pk } = options;
    this.setData({ pdgl_pk })
    wx.showLoading({
      title: '加载中...',
      mask: true
    })
    try {
      await Promise.all([
        // this.load4Grid(),
        this.getCyList(),
        this.getZsList(),
        this.getYlList(),
        this.getDatadictBVOsByCode()
      ]);
      await this.getBilldata();
    } catch (error) {
      console.error("异步操作失败:", error);
      // 这里可以添加错误处理（例如提示用户）
    } finally {
      wx.hideLoading(); // 确保无论成功失败都会隐藏loading
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  //新增行
  addRow(e) {
    const { dataName } = e.currentTarget.dataset;
    let arr = this.data[dataName];
    const obj = {
      'biz_1_pdgl_0': {
        CURRENT: true,
        billid: "",
        cy: null,
        cy_end: "",
        cy_pk: null,
        cy_start: "",
        fk: "",
        ifref: null,
        jl_is_del: 0,
        pk_corp: null,
        rowno: 20,
        rq: 1,
        sjd_code: "4",
        src_billid: null,
        src_billtype: null,
        src_rowid: null,
        ts: "",
        yl: null,
        yl_pk: null,
        zs: "",
        zs_pk: "",
        zyd: null,
        zyd_pk: null
      },
      'biz_1_pdgl_1': {
        CURRENT: true,
        billid: "",
        fk: "",
        ifref: null,
        jkqk: "",
        jl_is_del: 0,
        nl: "",
        pk_corp: "",
        rowno: 10,
        sfjb: "",
        sjhm: "",
        src_billid: null,
        src_billtype: null,
        src_rowid: null,
        ts: "",
        xb: "1",
        xbIndex: 0,
        xm: ""
      },
      'biz_1_pdgl_2': {
        CURRENT: true,
        billid: "",
        cy: "",
        cy_end: null,
        cy_pk: "",
        cy_start: null,
        fk: "",
        ifref: null,
        jl_is_del: 0,
        pk_corp: null,
        rowno: 10,
        rq: 1,
        sjd_code: "2",
        src_billid: null,
        src_billtype: null,
        src_rowid: null,
        ts: "",
        yl: null,
        yl_pk: null,
        zs: null,
        zs_pk: null,
        zyd: null,
        zyd_pk: null
      }
    }
    arr.push(obj[dataName]);
    this.setData({ [dataName]: arr })
  },
  //删除行
  deleteRow(e) {
    const { index, dataName } = e.currentTarget.dataset;
    let arr = this.data[dataName];
    arr.splice(index, 1)
    this.setData({ [dataName]: arr })
  },
  //时段选择
  sdPickerChange: function (e) {
    const { index, dataName } = e.currentTarget.dataset;
    let { sdList } = this.data;
    let arr = this.data[dataName];
    arr[index].sdIndex = e.detail.value;
    arr[index].sjd_code = sdList[e.detail.value].value;
    this.setData({ [dataName]: arr })
  },
  //性别选择
  xbPickerChange: function (e) {
    let { index, dataName } = e.currentTarget.dataset;
    let { xbList } = this.data;
    let arr = this.data[dataName];
    arr[index].xbIndex = e.detail.value;
    arr[index].xb = xbList[e.detail.value].value;
    this.setData({ [dataName]: arr })
  },
  //开始时间选择
  kssjTimeChange(e) {
    let { index, dataName } = e.currentTarget.dataset;
    let arr = this.data[dataName];
    arr[index].cy_start = e.detail.value;
    this.setData({ [dataName]: arr })
  },
  //结束时间选择
  kssjTimeChange(e) {
    let { index, dataName } = e.currentTarget.dataset;
    let arr = this.data[dataName];
    arr[index].cy_end = e.detail.value;
    this.setData({ [dataName]: arr })
  },
  //餐饮选择
  cyPickerChange(e) {
    const { index, dataName } = e.currentTarget.dataset;
    let { cyList } = this.data;
    let arr = this.data[dataName];
    arr[index].cyIndex = e.detail.value;
    arr[index].cy = cyList[e.detail.value].ctmc;
    arr[index].cy_pk = cyList[e.detail.value].billid;
    this.setData({ [dataName]: arr })
  },
  //住宿选择
  zsPickerChange(e) {
    const { index, dataName } = e.currentTarget.dataset;
    let { zsList } = this.data;
    let arr = this.data[dataName];
    arr[index].zsIndex = e.detail.value;
    arr[index].cy = zsList[e.detail.value].ctmc;
    arr[index].cy_pk = zsList[e.detail.value].billid;
    this.setData({ [dataName]: arr })
  },
  //资源点选择
  openSelectZyd(e) {
    const { index, dataName } = e.currentTarget.dataset;
    this.setData({
      index,
      dataName,
      selectpointShow: true,
    })
    // 获取子组件实例
    const child = this.selectComponent('#selectpoint');
    // 调用子组件方法
    if (child) {
      child.init();
    }
  },
  //处理选择的资源点
  handleSelectZyd(e) {
    let { index, dataName } = this.data;
    let arr = this.data[dataName];
    if (e && e.detail) {
      const data = e.detail.data;
      arr[index].zyd = data.zyd;
      arr[index].zyd_pk = data.zyd_pk;
      this.setData({ [dataName]: arr })
    }
  },
  //派单
  submit() {
    const { pdgl_pk } = this.data;
  },
  //获取数据
  async getBilldata() {
    const { pdgl_pk, sdList, xbList, cyList } = this.data;
    return util.request('/hongpei/order/dispatch/getBilldata.json', {
      templateID: '102543262251ba28QLJ8',
      pk_process: '11fba4437e23457ab69f7f61091b5f72',
      billid: pdgl_pk,
      billType: 'pdgl'
    }).then(res => {
      if (res.success) {
        let HEADER = res.data.HEADER;
        let { biz_1_pdgl_0, biz_1_pdgl_1, biz_1_pdgl_2 } = res.data.BODY;
        HEADER.xcd_status_name = {
          "100": "待提交",
          "200": "待接单",
          "300": "待确认",
          "400": "已接单",
          "500": "进行中",
          "600": "已完成"
        }[HEADER.xcd_status] || '';

        biz_1_pdgl_0.forEach(item => {
          //设置时段下标
          sdList.forEach((i, k) => {
            item.sjd_code == i.value ? item.sdIndex = k : null;
          })
        })
        biz_1_pdgl_2.forEach(item => {
          //设置时段下标
          sdList.forEach((i, k) => {
            item.sjd_code == i.value ? item.sdIndex = k : null;
          })
          //设置餐饮下标
          cyList.forEach((i, k) => {
            item.cy == i.ctmc ? item.cyIndex = k : null;
          })
        })
        //设置性别下标
        biz_1_pdgl_1.forEach(item => {
          xbList.forEach((i, k) => {
            item.xb == i.value ? item.xbIndex = k : null;
          })
        })

        this.setData({
          biz_1_pdgl_0: res.data.BODY.biz_1_pdgl_0,
          biz_1_pdgl_1: res.data.BODY.biz_1_pdgl_1,
          biz_1_pdgl_2: res.data.BODY.biz_1_pdgl_2,
          HEADER
        })
      } else {
        util.showMsg({ msg: res.message || res.msg });
      }
    });
  },
  //获取时段枚举
  async getDatadictBVOsByCode() {
    return util.request('/sys_manage/datadict/getDatadictBVOsByCode.json?datatype_code=SJD',
      {}, "POST").then(res => {
        if (res.success) {
          this.setData({
            sdList: res.data.SJD
          })
        } else {
          util.showMsg({ msg: res.message || res.msg });
        }
      });
  },
  //获取时段枚举
  async load4Grid() {
    return util.request('/ref/common/bd/load4Grid.json', {
      refName: '专题课',
      pk_billtemplet: '102543262251ba28QLJ8',
      itemkey: 'zyd',
      PAGE_PARAM_START: 0,
      PAGE_PARAM_LIMIT: 15,
    }, "POST").then(res => {
      if (res.success) {

      } else {
        util.showMsg({ msg: res.message || res.msg });
      }
    });
  },
  //获取餐饮枚举
  async getCyList() {
    return util.request('/ref/common/bd/load4Grid.json?_dc' + Date.now(), {
      refName: '餐饮资源',
      pk_billtemplet: '102543262251ba28QLJ8',
      itemkey: 'cy',
      PAGE_PARAM_START: 0,
      PAGE_PARAM_LIMIT: 15,
    }, "POST").then(res => {
      if (res.success) {
        this.setData({ cyList: res.records });
      } else {
        util.showMsg({ msg: res.message || res.msg });
      }
    });
  },
  //获取住宿枚举
  async getZsList() {
    return util.request('/ref/common/bd/load4Grid.json?_dc' + Date.now(), {
      refName: '住宿资源',
      pk_billtemplet: '102543262251ba28QLJ8',
      itemkey: 'zs',
      PAGE_PARAM_START: 0,
      PAGE_PARAM_LIMIT: 15,
    }, "POST").then(res => {
      if (res.success) {
        this.setData({ zsList: res.records });
      } else {
        util.showMsg({ msg: res.message || res.msg });
      }
    });
  },
  //获取医疗枚举
  async getYlList() {
    return util.request('/ref/common/bd/load4Grid.json?_dc' + Date.now(), {
      refName: '医疗资源',
      pk_billtemplet: '102543262251ba28QLJ8',
      itemkey: 'yl',
      PAGE_PARAM_START: 0,
      PAGE_PARAM_LIMIT: 15,
    }, "POST").then(res => {
      if (res.success) {
        this.setData({ ylList: res.records });
      } else {
        util.showMsg({ msg: res.message || res.msg });
      }
    });
  }
})